'''
1.准备好所有的top250地址
2.遍历所有地址
3.代码优化
'''
import requests
from lxml import etree

class DouBan250:
    __base_url = 'https://movie.douban.com/top250?start={}&filter='
    __headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36'
        }
    __num = 1
    def __init__(self):
        self.__pages = [i * 25 for i in range(10)]
        self.__urls = []
        for i in self.__pages:
            self.__urls.append(self.__base_url.format(i))

    def __get_pge_data(self, url):
        response = requests.get(url=url, headers=self.__headers)
        page = response.text
        html = etree.HTML(page)
        data_list = html.xpath("//ol/li")
        for li in data_list:
            title = li.xpath('.//span[@class="title"][1]/text()')
            source = li.xpath('.//span[@class="rating_num"][1]/text()')
            sub = li.xpath('.//span[@class="inq"][1]/text()')
            evaluate = li.xpath('.//div[@class="star"]/span[last()]/text()')
            title0 = title[0] if len(title) > 0 else ''
            source0 = source[0] if len(source) > 0 else ''
            sub0 = sub[0] if len(sub) > 0 else ''
            evaluate0 = evaluate[0] if len(evaluate) > 0 else ''
            print(f'{self.__num}.电影名称:{title0},评分:{source0},类型:{sub0},评价人数:{evaluate0}')
            self.__num = self.__num + 1
    def run(self):
        for url in self.__urls:
            self.__get_pge_data(url)
        self.__num = 1


if __name__== '__main__':
    db250 = DouBan250()
    db250.run()

    print('\n'*3)
    print('第二次调用')
    db250.run()
